/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPanelConFondo.java
 *
 * Created on 12/05/2011, 14:58:43
 */
package vistas.components;

import comu.Utilitats;
import entity.Produccio;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

/**
 *
 * @author David
 */
public class JPanelConFondo extends javax.swing.JPanel {

    private Image imagen;
    int x1, y1, x2, y2;
    boolean editable;
    List<Produccio> llistaProduccions;
    Produccio produccio;

    /** Creates new form JPanelConFondo */
    public JPanelConFondo() {
        initComponents();
        this.imagen = null;
        x1 = 0;
        y1 = 0;
        x2 = this.getWidth();
        y2 = this.getHeight();
        editable = true;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        if (editable) {
            x1 = evt.getX();
            y1 = evt.getY();
            x2 = x1;
            y2 = y1;
            repaint();
        }
    }//GEN-LAST:event_formMousePressed

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        if (editable) {
            x2 = evt.getX();
            y2 = evt.getY();
            repaint();
        }
    }//GEN-LAST:event_formMouseReleased

    private void formMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseDragged
        formMouseReleased(evt);
    }//GEN-LAST:event_formMouseDragged

    public void setEditable(boolean editable) {
        this.editable = editable;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public void setImagen(Image nuevaImagen) {
        imagen = Utilitats.adabtarImatgenSinDeformar(this.getWidth(), this.getHeight(), nuevaImagen);
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(JPanelConFondo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setProduccio(Produccio produccio) {
        this.produccio = produccio;
        this.x1 = produccio.getPosx1();
        this.y1 = produccio.getPosy1();
        this.x2 = produccio.getPosx2();
        this.y2 = produccio.getPosy2();
    }

    @Override
    public void paint(Graphics g) {
        if (imagen != null) {
            g.drawImage(imagen, 0, 0, imagen.getWidth(this), imagen.getHeight(this), this);
            g.setColor(Color.red);
            g.drawRect(x1, y1, x2 - x1, y2 - y1);
            if (produccio != null) {
                if (produccio.getProductesId().getImg() != null) {
                    ImageIcon img = new ImageIcon(produccio.getProductesId().getImg());
                    g.drawImage(img.getImage(), x1 + ((x2 - x1) / 4), y1 + ((y2 - y1) / 4), (x2 - x1) / 2, (y2 - y1) / 2, null);
                }
            }
            g.setColor(Color.yellow);
            if (llistaProduccions != null) {
                for (Produccio produccio2 : llistaProduccions) {
                    int xx1 = produccio2.getPosx1();
                    int xx2 = produccio2.getPosx2();
                    int yy1 = produccio2.getPosy1();
                    int yy2 = produccio2.getPosy2();
                    g.drawRect(xx1, yy1, xx2 - xx1, yy2 - yy1);
                    if (produccio2.getProductesId().getImg() != null) {
                        ImageIcon img = new ImageIcon(produccio2.getProductesId().getImg());
                        g.drawImage(img.getImage(), xx1 + ((xx2 - xx1) / 4), yy1 + ((yy2 - yy1) / 4), (xx2 - xx1) / 2, (yy2 - yy1) / 2, null);
                    }
                }
            }
            setOpaque(false);
        } else {
            setOpaque(true);
        }
        super.paint(g);
    }

    public int getX1() {
        return x1;
    }

    public int getX2() {
        return x2;
    }

    public int getY1() {
        return y1;
    }

    public int getY2() {
        return y2;
    }

    public void setX1(int x1) {
        this.x1 = x1;
    }

    public void setX2(int x2) {
        this.x2 = x2;
    }

    public void setY1(int y1) {
        this.y1 = y1;
    }

    public void setY2(int y2) {
        this.y2 = y2;
    }

    public void setListaProduccions(List<Produccio> produccions) {
        this.llistaProduccions = produccions;
        repaint();
    }
}
